// pages/clocking/index.js
const app = getApp()
import {
  getGrade,
  postDKinfo,
  getInfo,
  uploadFfile,
  getPlace,
  getXzqList
} from "../../utils/request"
Page({
  /**
   * 页面的初始数据
   */
  data: {
    objectArray: {}, //年级
    objectArray2: {}, //打卡地区
    objectArray3: {}, //打卡详细地点
    linshiplace: [],
    perinfo: {}, //个人信息
    showtext: false, //显示textarea
    name: '',
    school: '',
    grade: '',
    area: '',
    place_id: '',
    place: '',
    url: '',
    lsurl: '',
    text: '',
    baseUrl: app.baseUrl,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getinfo()
    this.getdkdqList()
    this.getdkadList()
  },
  // 获取个人信息
  async getinfo() {
    const data = await getInfo();
    this.setData({
      perinfo: data.data,
      school: data.data.school_name,
      grade: data.data.class_name,
      name: data.data.real_name
    }, () => {
      this.getnianjiList()
    })
  },
  getValue(event){
    this.setData({
      text:event.detail.value
    })
  },
  // 获取年级列表
  async getnianjiList() {
    const data = await getGrade();
    var array = [];
    data.data.forEach(function (val, key, arr) {
      array.push({
        id: key,
        name: val.title
      })
    })
    this.setData({
      objectArray: {
        title: "请选择年级",
        array,
        value: this.data.perinfo.class_name
      }
    })
  },
  // 获取打卡地区列表
  async getdkdqList() {
    const data = await getXzqList();
    var array = [];
    data.data.forEach(function (val, key, arr) {
      array.push({
        id: key,
        name: val.area
      })
    })
    this.setData({
      objectArray2: {
        title: "请选择打卡地区",
        array
      }
    })
  },
  // 获取打卡详细地址列表
  async getdkadList() {
    const data = await getPlace({ keyword: this.data.area });
    var array = [];
    data.data.forEach(function (val, key, arr) {
      array.push({
        id: key,
        name: val.name
      })
    })
    this.setData({
      linshiplace: data.data,
      objectArray3: {
        title: "请选择打卡详细地址",
        array
      }
    })
  },
  jumpSlect() {
    const { area } = this.data
    if (area === '') {
      app.func.Toast.none("请先选择地区")
      return
    }
  },
  // 隐藏textarea
  showmodel(e) {
    this.setData({
      showtext: e.detail
    })
  },
  // 页面提交数据
  updataname(e) {
    this.setData({
      name: e.detail.value
    })
  },
  updataschool(e) {
    this.setData({
      school: e.detail.value
    })
  },
  updataNj(e) {
    this.setData({
      grade: e.detail,
      showtext: false
    })
  },
  updataDk(e) {
    this.setData({
      area: e.detail,
      showtext: false
    }, () => {
      this.getdkadList()
    })
  },
  updataAd(e) {
    const arr = this.data.linshiplace;
    arr.forEach((item) => {
      if (item.name == e.detail) {
        this.setData({
          place_id: item.id
        })
      }
    })
    this.setData({
      place: e.detail,
      showtext: false
    })
  },
  updataaddress(e) {
    this.setData({
      text: e.detail.value
    })
  },

  // 上传图片
  chooseImage() {
    const that = this;
    if (that.data.url) {
      return
    }
    wx.chooseImage({
      count: 1,
      success: function (res) {
        that.setData({
          lsurl: res.tempFilePaths[0]
        })
        wx.showLoading({
          title: '正在上传',
          mask: true,
        })
        wx.uploadFile({
          url: 'https://zdhapi.fly-network.com/api/?method=upload_file',
          filePath: that.data.lsurl,
          name: 'content',
          formData: {
            'type': 'all'
          },
          success: function (res) {
            var obj = res.data;
            obj = JSON.parse(obj)
            wx.hideLoading({
              success: (res) => {
                console.log(res)
              },
            })
            that.setData({
              url: obj.data.img_url[0]
            })
          }
        })
        // var data=uploadFfile({type:'all'})
      },
    })
  },
  // 删除图片
  delpic() {
    this.setData({
      url: ''
    })
  },
  updata: app.func.throttle(async function () {
    const res = {
      name: this.data.name,
      school: this.data.school,
      grade: this.data.grade,
      area: this.data.area,
      place_id: this.data.place_id,
      place: this.data.place,
      url: this.data.url,
      text: this.data.text
    }
    const data = await postDKinfo(res);

    if (data.code !== '1') return
    // app.func.Toast.success("提交成功")
      wx.requestSubscribeMessage({
        tmplIds: ['bY3ze2a0tFUsIRuyRptvAFWJhg7BKnmnRTZfS3XCj0s', '107tgLOct49jzx3ylrLLK3WHLtUokL3kbEfA7R1yOsU', 'AJXvR03taERIgAFBVS0iN8c4ypMRUL34LohsvdSSyMs'],
        success(res) {
          setTimeout(() => {
            wx.navigateTo({
              url: '/pages/clockresult/index',
            })
          }, 1000)
        },
        fail: (error) => {
          setTimeout(() => {
            wx.navigateTo({
              url: '/pages/clockresult/index',
            })
          }, 1000)
        }
      })
  }),
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})